package com.sn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sn.core.component.page.Pagination;
import com.sn.core.emums.SexEnum;
import com.sn.core.emums.UserTypeEnum;
import com.sn.core.util.EnumUtil;
import com.sn.core.util.pub.exception.DBExceptionEnums;
import com.sn.core.util.pub.exception.ServiceException;
import com.sn.dao.ScoreMapper;
import com.sn.dao.UserMapper;
import com.sn.model.Customer;
import com.sn.model.CustomerScore;
import com.sn.model.req.CustomerReq;
import com.sn.model.vo.CustomerVo;
import com.sn.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by qingshengzheng on 18/6/20.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ScoreMapper scoreMapper;

    @Transactional
    public void addCustomer(Customer customer) {
        customer.setCreatetime(System.currentTimeMillis());
        customer.setPassword("123456");
        customer.setUpdatetime(System.currentTimeMillis());
        userMapper.addCustomer(customer);
        CustomerScore userscore = new CustomerScore();
        userscore.setCid(customer.getCid());
        userscore.setCreatetime(System.currentTimeMillis());
        userscore.setUpdatetime(System.currentTimeMillis());
        userscore.setMscore(0l);
        userscore.setTscore(0l);
        scoreMapper.addScore(userscore);


    }

    public List<Customer> queryVipSearchList(String keyword,Integer userType) {
        return userMapper.queryVipSearchList(keyword,userType);
    }


    public Pagination queryCustomer(CustomerReq req) {
        Pagination pagination = new Pagination(req.getPageno(), req.getPagesize());
        Page<?> page = PageHelper.offsetPage(pagination.getStart(), pagination.getPageSize());
        List<CustomerVo> vos = userMapper.queryCustomers(req);
        for(CustomerVo vo:vos){
            String sexDesc = StringUtils.isBlank(vo.getSex()) ?"": EnumUtil.getByCode(SexEnum.class, vo.getSex()).getDesc();
            String usertypeDesc=StringUtils.isBlank(vo.getUsertype()+"") ?"": EnumUtil.getByCode(UserTypeEnum.class, vo.getUsertype()+"").getDesc();
            vo.setSexdesc(sexDesc);
            vo.setUsertypedesc(usertypeDesc);
        }
        pagination.setTotalCount(page.getTotal());
        pagination.setV(vos);
        return pagination;
    }


    public void deleteCustomer(Long id) {
        userMapper.deleteCustomer(id);

    }

    public CustomerVo detailByname(String truename) {
        CustomerVo vo = userMapper.queryCustomerByTruename(truename);
        return vo;
    }

    public CustomerVo detail(Long cid) {
        CustomerVo vo = userMapper.detail(cid);
        return vo;
    }


    public void updateCustomer(Customer customer) {
        userMapper.updateCustomer(customer);

    }

    public CustomerVo login(CustomerReq req) {
        CustomerVo customer = detailByname(req.getTruename());
        if (customer == null) throw new ServiceException(DBExceptionEnums.USER_NO_EXISTS);
        else if (!req.getPassword().equals(customer.getPassword()))
            throw new ServiceException(DBExceptionEnums.USER_PASSWORD_ERROR);
        return customer;
    }

    public void updatePwd(CustomerReq req) {
        userMapper.updatePwd(req.getCid(), req.getPassword());

    }

}
